package com.lucifer.cloud.boot.blog.chat.support;

import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.dashscope.common.MultiModalMessage;
import com.alibaba.dashscope.common.Role;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;

/**
 * @author lucifer
 * @date 2025/3/24 11:05
 */
@Service
public class AliDashScopeSupportApi {


    @Value("${spring.ai.dashscope.api-key}")
    private  String dashScopeApiKey;

    @Value("${spring.ai.dashscope.chat.options.model}")
    private  String dashScopeChatModel;

    @Value("${spring.ai.dashscope.voice.options.model}")
    private  String dashScopeVoiceModel;

    @Value("${spring.ai.dashscope.embedding.options.model}")
    private  String dashScopeEmbeddingModel;

    @SneakyThrows
    public String voice(String audio) {
        MultiModalConversation conv = new MultiModalConversation();
        MultiModalMessage userMessage = MultiModalMessage.builder()
                .role(Role.USER.getValue())
                .content(Arrays.asList(
                        Collections.singletonMap("audio", audio)))
                .build();
        MultiModalConversationParam param = MultiModalConversationParam.builder()
                .apiKey(dashScopeApiKey)
                .model(dashScopeVoiceModel)
                .message(userMessage)
                .build();
        MultiModalConversationResult result = conv.call(param);
        String text = String.valueOf(result.getOutput().getChoices().get(0).getMessage().getContent().get(0).get("text"));
        return text;
    }
}
